﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DomainModel;

namespace BL
{
    public static class BLOrders
    {
        //Insert orders
        public static void InsertOrders(Order order, ref List<string> errors)
        {

            //check if order is null
            if (order == null)
            {
                BLMsgPool.ErrorCapsule(3, 1, ref errors);
                return;
            }

            //check if customer is valid
            if (BLCustomers.GetCustomerDetail(order.customer_id, ref errors) == null)
            {
                BLMsgPool.ErrorCapsule(3, 2, ref errors);
                return;
            } 


            if (errors.Count > 0)
                return;

            DALOrders.InsertOrders(order, ref errors);
           
        }

        //Get Order total price from Orders
        public static double GetTotalPrice(int id, ref List<string> errors)
        {

            //if id is not valid
            if (BLTool.isNotValidID(id))
            {
                BLMsgPool.ErrorCapsule(3, 3, ref errors);
                return 0;
            }

            //check if customer is valid
       /*     if (BLCustomers.GetCustomerDetail(id, ref errors) == null)
            {
                BLMsgPool.ErrorCapsule(3, 2, ref errors);
                return 0;
            } */

            if (errors.Count > 0)
                return 0;
            return DALOrders.GetOrderTotalPrice(id, ref errors);
        }
    
        //insert order items
        public static void InsertOrderItems(OrderItem item, ref List<string> errors)
        {
            List<OrderItem> orderitem = null;
            int i = 0;

            //test if orderitem is null
            if (item == null)
            {
                BLMsgPool.ErrorCapsule(3, 4, ref errors);
                return;
            }

            //test if order id is valid 
            if (BLTool.isNotValidID(item.order_id))
            {
                BLMsgPool.ErrorCapsule(3, 3, ref errors);
                return;
            }

            //test if product id is valid
            if (!(BLTool.isValidProductId(item.product_id)))
            {
                BLMsgPool.ErrorCapsule(3, 5, ref errors);
                return;
            }
            if(BLTool.isNagetive( item.item_state))
            {
                BLMsgPool.ErrorCapsule(1, 13 , ref errors);
                return;
            }

            orderitem = BLOrders.GetOrderProducts(item.order_id, ref errors);
            //test if product id is already in the orders list
            if (orderitem != null)
            {
                for (i = 0; i < orderitem.Count; i++)
                {

                    if (item.product_id == orderitem[i].product_id)
                    {
                        BLMsgPool.ErrorCapsule(3, 7, ref errors);
                        return;
                    }
                }
            }
            item.sub_total = BLTv.GetProductPrice(item.product_id, ref errors);
            BLOrders.CalculateSubTotal(item, ref errors);
            item.AddDiscount();
            item.DailyDeal();
            item.BuyOneGetOneFree();
            DALOrders.InsertOrderItems(item, ref errors);
            BLOrders.SetOrderTotalPrice(item.order_id, ref errors);
        }

        public static double CalculateSubTotal(OrderItem item, ref List<string> errors)
        {
            item.sub_total = (double) item.quantity * item.sub_total;
            return item.sub_total;
        }

        public static void SetOrderTotalPrice(int id, ref List<string> errors)
        {
            DALOrders.SetOrderTotalPrice(id, ref errors);
        }

        //delete order items
        public static void DeleteOrderItem(int oid, int pid, ref List<string> errors)
        {
            List<OrderItem> orderitems = null;
            int i = 0;
            int exists = 1;
            //check if order id is valid
            if (BLTool.isNotValidID(oid))
            {
                BLMsgPool.ErrorCapsule(3, 3, ref errors);
                return;
            }

            //check if product id is valid
            if (!BLTool.isValidProductId(pid))
            {
                BLMsgPool.ErrorCapsule(3, 5, ref errors);
                return;
            }

            //check if order id exists
     /*       if (oid >= BLOrders.NumOrderItems(ref errors))
            {
                BLMsgPool.ErrorCapsule(3, 8, ref errors);
                return;
            }  */
            
            //check if product id exists
            
            orderitems = BLOrders.GetOrderProducts(oid, ref errors);

            if (orderitems != null)
            {
                for (i = 0; i < orderitems.Count; i++)
                {
                    if (orderitems[i].product_id == pid)
                        exists = 1;
                }
            }
            if (exists == 0)
            {
                BLMsgPool.ErrorCapsule(3, 9, ref errors);
                return;
            }

            DALOrders.DeleteOrderItem(oid, pid, ref errors);
            BLOrders.SetOrderTotalPrice(oid, ref errors);
        }

        //deletes all order items from an order
        public static void ClearOrderItems(int oid, ref List<string> errors)
        {
            List<OrderItem> orderitem = null;
            int i = 0;

            //check if order id is valid
            if (BLTool.isNotValidID(oid))
            {
                BLMsgPool.ErrorCapsule(3, 3, ref errors);
                return;
            }

            //check if order id exists
            if (oid >= BLOrders.NumOrderItems(ref errors))
            {
                BLMsgPool.ErrorCapsule(3, 8, ref errors);
                return;
            }

            orderitem = BLOrders.GetOrderProducts(oid, ref errors);
            for( i = 0; i < orderitem.Count; i++)
            {
                BLOrders.DeleteOrderItem(oid, orderitem[i].product_id, ref errors);
            }


        }

        //gets product in orderitems list
        public static List<OrderItem> GetOrderProducts(int id, ref List<string> errors)
        {


            if (BLTool.isNotValidID(id))
            {
                BLMsgPool.ErrorCapsule(3, 3, ref errors);
                return null;
            }
            return DALOrders.GetOrderProducts(id, ref errors);
        }

        public static List<Order> GetCustomerOrders(int id, ref List<string> errors)
        {
            if (BLTool.isNotValidID(id))
            {
                BLMsgPool.ErrorCapsule(3, 3, ref errors);
                return null;
            }
            return DALOrders.GetCustomerOrders(id, ref errors);

        }

        //gets order history of customer
        public static List<OrderItem> SeeMyHistory(int id, ref List<string> errors)
        {
            //test if id is null
 
            //is id is not valid
            if (BLTool.isNotValidID(id))
            {
                BLMsgPool.ErrorCapsule(3, 3, ref errors);
                return null;
            }
            //check if customer is valid
            if (BLCustomers.GetCustomerDetail(id, ref errors) == null)
            {
                BLMsgPool.ErrorCapsule(3, 2, ref errors);
                return null;
            }

            return DALOrders.SeeMyHistory(id, ref errors);
        }

        //deletes all customers and all transactions
        public static void DeleteAllRecords(ref List<string> errors)
        {
            if (errors.Count > 0)
                return;

            DALOrders.DeleteAllRecords(ref errors);

        }

        //returns the number of order items 
        public static int NumOrderItems(ref List<string> errors)
        {
            return DALOrders.numOrderItemEntries(ref errors);
        }

        //returns the number of orders
        public static int NumOrderEntries(ref List<string> errors)
        {
            return DALOrders.numOrderEntries(ref errors);
        }
    
        //places an order
        public static void PlaceOrder(int order_num, ref List<string> errors)
        {
            //checks that order number exists

         //   if (BLTool.isValidProductId( order_num) )
          //  {
            //    BLMsgPool.ErrorCapsule(4, 8, ref errors);
              //  return;
            //}

            DALOrders.PlaceOrder(order_num, ref errors);
        }

        //see if order is placed
        public static int SeeIfOrderPlaced(int order_num, ref List<string> errors)
        {

            if (BLTool.isValidProductId( order_num) )
            {
                BLMsgPool.ErrorCapsule(4, 8, ref errors);
                return -1;
            }
            if(order_num > DALOrders.numOrderEntries(ref errors))
            {
                BLMsgPool.ErrorCapsule(4,8, ref errors);
                return -1;
            }
          
            return DALOrders.SeeIfOrderPlaced(order_num, ref errors);
        }
    }
}
